package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理相关接口")
public class DishController {

    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(value = "category_dish",key = "#dishDTO.categoryId")
    public Result insertDishWithFlavors(@RequestBody DishDTO dishDTO){
        log.info("添加菜品，菜品数据{}",dishDTO);
        dishService.insertDishWithFlavors(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页条件查询
     */
    @GetMapping("/page")
    @ApiOperation("菜品的 分页条件查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品的 分页调价查询，条件分页数据:{}",dishPageQueryDTO);

        PageResult pageResult= dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 批量删除菜品
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @CacheEvict(value = "category_dish",allEntries = true)
    public Result deleteByIds(@RequestParam("ids")List<Long> ids){
        log.info("批量删除菜品，要删除的id有：{}",ids);
        dishService.deleteById(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品的所有信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
@ApiOperation("根据id查询菜品的所有信息")
public Result<DishVO> selectById(@PathVariable Long id){
     log.info("根据id查询菜品的所有信息，id为:{}",id);
     DishVO dishVO =dishService.selectById(id);
     return Result.success(dishVO);
}



    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(value = "category_dish",key = "#dishDTO.categoryId")
    public Result updateDishWithFlavors(@RequestBody DishDTO dishDTO){
        log.info("修改菜品，菜品数据{}",dishDTO);
        dishService.updateDishWithFlavors(dishDTO);
        return Result.success();
    }
    /*8
    根据分类查询起售中菜品
     */
@GetMapping("/list")
@ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> listByCateGoryId(Long categoryId){
        log.info("根据分类查询起售中菜品;{}",categoryId);
        List<Dish> list=dishService.listByCategoryId(categoryId);
        return Result.success(list);
    }



/*    *//**
     * 起售和禁售菜品
     * @param status
     * @param id
     * @return
     *//*
    @PostMapping("/status/{status}")
@ApiOperation("起售或停售菜品")
public Result<String> startOrStop(@PathVariable Integer status,Long id){
        log.info("起售或停售菜品，{},id:{}",status,id);
        dishService.startOrStop(status,id);
    Set keys = redisTemplate.keys("dish_*");
    redisTemplate.delete(keys);
    return  Result.success();

}*/

    /**
     * 起售或停售菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售或停售菜品")
    @CacheEvict(value = "category_dish",allEntries = true)
    public Result startOrStop(@PathVariable("status")Integer status,Long id){
        log.info("启用或禁用，id为； {},{}", status, id);
        dishService.startOrStop(status,id);
        return Result.success();
    }


}
